package com.coderside.fragment.String;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringUtil {

	/**
	 * 验证字符串content是否匹配正则表达式regEx
	 * @param regEx 正则表达式的字面量
	 * @param content 要匹配的字符串
	 * @return
	 */
	public static boolean matcherReg(String regEx, String content) {
		Pattern p = Pattern.compile(regEx, Pattern.CASE_INSENSITIVE);
		Matcher m = p.matcher(content);
		return m.find();
	}

	/**
	 * 去掉字符串中的TAB、回车；\t：TAB；\r\n:回车；
	 * @param str 输入的字符串
	 * @return
	 */
	public static String replaceSpecialCharacter_t_r_n(String str) {
		Pattern pt = Pattern.compile("\t|\r|\n");
		Matcher mt = pt.matcher(str);
		return mt.replaceAll("");
	}

	/**
	 * 去掉字符串中的空格、TAB、回车；\\s*：空格；\t：TAB；\r\n:回车；
	 * @param str 输入的字符串
	 * @return
	 */
	public static String replaceSpecialCharacter_s_t_r_n(String str) {
		Pattern pt = Pattern.compile("\\s*|\t|\r|\n");
		Matcher mt = pt.matcher(str);
		return mt.replaceAll("");
	}

	/**
	 * 根据正则表达式 拆分字符串、包含匹配字符串
	 * @return
	 */
	public static String[] splitString(String regEx, String content) {
		List<String> list = new ArrayList<String>();
		Pattern p = Pattern.compile(regEx, Pattern.CASE_INSENSITIVE);
		Matcher m = p.matcher(content);
		int index = 0;
		while (m.find()) {
			String match = "";
			if (index != 0) {
				match = content.subSequence(index - 2, m.start()).toString();
				list.add(match);
			} else {
				match = content.subSequence(index, m.start()).toString();
			}
			index = m.end();
		}
		String endcontent = content.subSequence(index - 2, content.length()).toString();
		list.add(endcontent);
		String[] res = new String[list.size()];
		return list.toArray(res);
	}

	/**
	 * 判断是否是数字，可以允许-号，小数点
	 * 
	 * @author 193341
	 * @param value
	 * @return
	 */
	public boolean isNumber(String value) {
		boolean flag = false;
		try {
			java.util.regex.Pattern pattern = java.util.regex.Pattern.compile("^\\-?[0-9]{1,}\\.?[0-9]{1,}$");
			java.util.regex.Matcher match = pattern.matcher(value);
			flag = match.matches();

		} catch (Exception e) {

		}
		return flag;
	}

}
